import { defineStore } from 'pinia'

import { loadStorage, loadCookie, LOGIN_STATUS, ACCESS_TOKEN, REFRESH_TOKEN, USER_INFO, REGISTER_FLAG } from '../utils/cache'
const { MODE } = import.meta.env

export const useStore = defineStore('main', {
  state: () => {
    return {
      isShowLoadding: false,
      loginStatus: 0,
      accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjo1MDIwLCJsb2dpbl90eXBlIjo5LCJvcGVuSWQiOiJvT3NjRzZhQjNWbWJDa1NGWTRhRjJLRTBhWUFjIiwic2VydmVyX21vZGUiOiJkZWJ1ZyIsImlzcyI6ImF4ZV9tYWdpY19taXJyb3IiLCJleHAiOjE3NDA3NDgwODJ9.80m_GjhhP7OhjEnTuUhW77D6kBxaP8ClzKNDj3qXZMM',
      userInfo: {},
    }
  },
  getters: {},
  actions: {
    setLoginStatus(query) {
      if ((query === 0 || query === 1) && MODE === 'prod') {
        this.accessToken = ''
        this.userInfo = {}
      }
      this.loginStatus = query
      // commit(types.SET_LOGIN_STATUS, saveCookie(LOGIN_STATUS, query, { expires: 7 }))
    },
    setUserInfo(query) {
      this.userInfo = query
    },
    setAccessToken(query) {
      this.accessToken = query
    }
  },
  persist: {
    // storage: localStorage,
    // paths: ['loginStatus', 'accessToken', 'userInfo']
  }
})
// vue使用
/**
 *  import { storeToRefs } from "pinia";
 *  import { useStore } from '../store/index'
 *   const user = useStore();
 *  const { count, status } = storeToRefs(use)
 * 
 * 使用$path 修改多条状态数据
 *  user.$patch((state)=>{
        {
        state.status = user.count > 10 ? false : true,
        state.count = user.status ? user.count+1:user.count-1
    }
    })

 *  */
